package com.gopay.checkorder.service.impl.bcdl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.common.Result;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.BcdlMessage;
import com.gopay.common.domain.bank.BcdlResult;
import com.gopay.common.domain.bank.dayend.BaseDayendEntry;
import com.gopay.common.domain.bank.pab.bcdl.res.ResPAB4005;
import com.gopay.common.domain.cashier.BankCardType;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.domain.cps.vo.InAccInfo;
import com.gopay.remote.bank.BcdlInteractRemoteService;

/**
 * 平安银行银企直连对单
 * 
 * @CheckBcdlOrderPABImpl.java
 * @author liujy
 * @2013-3-20 上午11:12:29 www.gopay.com.cn Inc.All rights reserved.
 */
public class CheckBcdlOrderPABImpl implements BankOrderService {
    private final static Logger logger = LoggerFactory.getLogger(CheckBcdlOrderPABImpl.class);

    @Resource(name = "bankBcdlClient")
    private BcdlInteractRemoteService bankBcdlClient;

    @Override
    public CheckOrderResult checkOrder(BankOrder bankOrder, String target) {
        CheckOrderResult checkOrderResult = new CheckOrderResult();
        checkOrderResult.setOrderId(bankOrder.getOrderId());

        BcdlMessage base = new BcdlMessage();
        base.setOrderId(bankOrder.getOrderId());// 使用订单号
        base.setOutOrderId(bankOrder.getOutOrderId());// 使用外部订单号
        base.setBank(bankOrder.getBankCode());

        BcdlResult result = new BcdlResult();
        
        logger.error("bcdl check order BcdlMessage: " + base);
        
        try {
            result = bankBcdlClient.query(base);
        } catch (Exception e) {
            logger.error("银企直连订单查询失败: " + base.getOrderId(), e);
            checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
            checkOrderResult.setErrMsg("bis is too busy.");
            return checkOrderResult;
        }
        
        logger.error("bcdl check order result: " + result);

        //modified by zyt,2017-10-10, 将result为空和其他情况分开写，解决之前null中依然get的潜在空指针
        if (result == null) {
            logger.error("bankBcdlClient fail. result is null");
            return checkOrderResult;
        } else if (!result.isSuccess() || !ProcCodeConstants.PROC_CODE_100S1000.equals(result.getResultCode())) {
            logger.error("bankBcdlClient fail.");
            logger.error(ToStringBuilder.reflectionToString(result, ToStringStyle.DEFAULT_STYLE));
            checkOrderResult.setErrCode(result.getResultCode());
            checkOrderResult.setErrMsg(StringUtils.isNotBlank(result.getBankRetMsg()) ?
                    result.getBankRetMsg() : result.getResultCode());
            return checkOrderResult;
        }

        ResPAB4005 res4005 = (ResPAB4005) result.getDefaultModel();
        if (res4005 == null) {
            // 查询订单为空
            logger.error("fail:orderId:" + bankOrder.getOrderId() + " errcode:" + ProcCodeConstants.PROC_CODE_100E5062
                    + " err:bis query is null.");
            checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100E5062);
            checkOrderResult.setErrMsg("bis query is null.");
        }
        
        InAccInfo accInfo = new InAccInfo();
        accInfo.setOrderId(bankOrder.getOrderId());
        accInfo.setAmount(new BigDecimal(res4005.getTranAmount()));
        accInfo.setRetCode("00");
        accInfo.setCardType(BankCardType.Unknown.toString());
        
        checkOrderResult.setAccInfo(accInfo);
        checkOrderResult.setSuccess(true);
        checkOrderResult.setErrCode(ProcCodeConstants.PROC_CODE_100S1000);
        return checkOrderResult;
    }

	@Override
	public BankOrder getDbOrder() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BaseMessage dayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<CheckOrderResult> checkDayend(String bank_bus) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result getDayendMsg(String string) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public CheckOrderResult checkDayendOrder(BaseDayendEntry bdm, BankOrder bo) {
		// TODO Auto-generated method stub
		return null;
	}

}
